// ==UserScript==
// @name       YE功能增强.01 - [QQ邮箱]
// @version    150628.03.01.004
// @description  Esc按键折叠QQ订阅文章，打开等
// @namespace  http://AsionTang.CN
// @copyright  2013+, AsionTang
// @iconURL    http://qq.com/favicon.ico
// @downloadURL	https://bitbucket.org/AsionTang/52scriptlibrary/raw/default/YeJS/01.QQ.user.js
// @match      https://mail.qq.com/cgi-bin/frame_html?*
// @grant      none
// ==/UserScript==
(function() {
    function downloadAsCsv(text, fileName) {
        var blob = new Blob([text],{
            type: "text/csv;charset=UTF-8"
        });
        var aLink = document.createElement('a');
        aLink.href = URL.createObjectURL(blob);
        aLink.download = fileName + '.CSV';
        aLink.click();
    }
    //方案2:通过抓去特殊接口,来实现获取所有图片地址
    (function() {
        function viewer_Callback(data) {
            var photoUrls = '';
            var countRaw = 0;
            var countOrigin = 0;
            var countNormal = 0;
            for (var i = 0; i < data.data.photos.length; i++) {
                var p = data.data.photos[i];
                var tmp;
                if (p.raw) {
                    //存在raw链接,优先使用.
                    tmp = p.raw;
                    countRaw++;
                } else if (p.origin) {
                    //使用大图
                    tmp = p.origin;
                    countOrigin++;
                } else {
                    //最后没办法使用小图.
                    tmp = p.url;
                    countNormal++;
                }
                tmp += "&save=1&d=1";
                //
                photoUrls += tmp;
                photoUrls += '\r\n';
            }
            //
            console.log('total:' + data.data.photos.length);
            console.log('countRaw:' + countRaw);
            console.log('countOrigin:' + countOrigin);
            console.log('countNormal:' + countNormal);
            //
            downloadAsCsv(photoUrls, 'all');
        }
        //==============================================================================
        // 操作步骤:
        //      1.进入QQ空间相册,使用普通缩略图模式
        //      2.打开F12调试工具 - Network - JS - 过滤字符串:cgi_floatview_photo_list_v2
        //      3.查看某张图片大图 - 即可拿到URL,然后在所有可能的数字后加00
        //      4.得到JSON后哦,粘贴在下方.运行即可.
        //==============================================================================
        // 将接口里的数据粘贴在此之后:
    }
    )();
    //方案1:通过直接解析缩略图视图,来下载.
    (function() {
        var count = 0;
        var photoUrlsOrigin = "";
        var photoUrlsRaw = "";
        var items = document.getElementsByTagName('img');
        console.log('Total Count:' + items.length);
        for (var i = 0; i < items.length; i++) {
            var item = items[i];
            if (item.src.indexOf('psb') == -1 || item.src.indexOf('/m/') == -1) {
                //console.log(item.src);
                continue;
            }
            count++;
            //0.缩略图地址：
            //      http://b188.photo.store.qq.com/psb?/V13fOWST2EsSDU/m3BZfzi6tZh5hULOkoDFgvbi8RWM1PgD1aqehOF6Ve8!/m/dAUyEHDiDwAAnull&bo=LwQgA0AGqwQFCMo!&rf=photolist&t=5
            //  点击“下载图片”功能：
            //      http://r.photo.store.qq.com/psb?/V13fOWST2EsSDU/m3BZfzi6tZh5hULOkoDFgvbi8RWM1PgD1aqehOF6Ve8!/o/dAUyEHDiDwAA&bo=LwQgA0AGqwQFAMI!&rf=viewer_4&save=1&d=1
            //  测试拼接URL下载:域名改为：r.photo.store.qq.com；null之后改为&save=1&d=1；m该为b
            //      http://r.photo.store.qq.com/psb?/V13fOWST2EsSDU/m3BZfzi6tZh5hULOkoDFgvbi8RWM1PgD1aqehOF6Ve8!/b/dAUyEHDiDwAA&save=1&d=1
            //  测试拼接URL下载:域名改为：r.photo.store.qq.com；null之后改为&save=1&d=1；m该为o
            //      http://r.photo.store.qq.com/psb?/V13fOWST2EsSDU/m3BZfzi6tZh5hULOkoDFgvbi8RWM1PgD1aqehOF6Ve8!/o/dAUyEHDiDwAA&save=1&d=1
            //  测试结果:手动下载和o的方式下载的图片MD5一致.b的方式图片尺寸很小.
            //
            //1.使用“普通”模式上传的图片:在相册缩略图情况下:
            //      http://a3.qpic.cn/psb?/V13fOWST08XyuI/6.e8J4OG66TL62Qnb9e6QY808TRaxPcZi5QS28kISOM!/m/dAoBAAAAAAAAnull&bo=sARABrAEQAYFByQ!&rf=photolist&t=5
            //  点击"下载图片"功能:
            //      http://a3.qpic.cn/psb?/V13fOWST08XyuI/6.e8J4OG66TL62Qnb9e6QY808TRaxPcZi5QS28kISOM!/b/dAoBAAAAAAAA&bo=sARABrAEQAYFACM!&rf=viewer_4&save=1&d=1
            //  测试拼接URL下载:域名改为：r.photo.store.qq.com；null之后改为&save=1&d=1；m该为b
            //      http://r.photo.store.qq.com/psb?/V13fOWST08XyuI/6.e8J4OG66TL62Qnb9e6QY808TRaxPcZi5QS28kISOM!/b/dAoBAAAAAAAA&save=1&d=1
            //  测试拼接URL下载:域名改为：r.photo.store.qq.com；null之后改为&save=1&d=1；m该为o
            //      http://r.photo.store.qq.com/psb?/V13fOWST08XyuI/6.e8J4OG66TL62Qnb9e6QY808TRaxPcZi5QS28kISOM!/o/dAoBAAAAAAAA&save=1&d=1
            //  测试结果:三种方式下载的图片MD5一致.
            //
            //2.使用“高清”模式上传的图片:在相册缩略图情况下:
            //      http://a1.qpic.cn/psb?/V13fOWST08XyuI/4Q1iWEvsXcprfyYQ8CAdMmBODAWmBTr75hiLWw43pPI!/m/dAwBAAAAAAAAnull&bo=gAJVAwAAAAAFB*A!&rf=photolist&t=5
            //  点击"下载图片"功能:
            //      http://a2.qpic.cn/psb?/V13fOWST08XyuI/4Q1iWEvsXcprfyYQ8CAdMmBODAWmBTr75hiLWw43pPI!/b/dAwBAAAAAAAA&bo=gAJVAwAAAAAFAPc!&rf=viewer_4&save=1&d=1
            //  测试拼接URL下载:域名改为：r.photo.store.qq.com；null之后改为&save=1&d=1；m该为b
            //      http://r.photo.store.qq.com/psb?/V13fOWST08XyuI/4Q1iWEvsXcprfyYQ8CAdMmBODAWmBTr75hiLWw43pPI!/b/dAwBAAAAAAAA&save=1&d=1
            //  测试拼接URL下载:域名改为：r.photo.store.qq.com；null之后改为&save=1&d=1；m该为o
            //      http://r.photo.store.qq.com/psb?/V13fOWST08XyuI/4Q1iWEvsXcprfyYQ8CAdMmBODAWmBTr75hiLWw43pPI!/o/dAwBAAAAAAAA&save=1&d=1
            //  测试结果:三种方式下载的图片MD5一致.
            //
            //3.使用“原图”模式上传的照片:在相册缩略图情况下
            //      http://a3.qpic.cn/psb?/V13fOWST08XyuI/5621kPoToBNnmYfEopm7q9AOM61kYUEi6NzNzcGTcqA!/m/dNoAAAAAAAAAnull&bo=OASgBYAHAAoFCT4!&rf=photolist&t=5
            //  点击"下载图片"功能:
            //      http://r.photo.store.qq.com/psb?/V13fOWST08XyuI/5621kPoToBNnmYfEopm7q9AOM61kYUEi6NzNzcGTcqA!/r/dNoAAAAAAAAA&save=1&d=1
            //  测试拼接URL下载:域名改为：r.photo.store.qq.com；null之后改为&save=1&d=1；m该为b
            //      http://r.photo.store.qq.com/psb?/V13fOWST08XyuI/5621kPoToBNnmYfEopm7q9AOM61kYUEi6NzNzcGTcqA!/b/dNoAAAAAAAAA&save=1&d=1
            //  测试拼接URL下载:域名改为：r.photo.store.qq.com；null之后改为&save=1&d=1；m该为o
            //      http://r.photo.store.qq.com/psb?/V13fOWST08XyuI/5621kPoToBNnmYfEopm7q9AOM61kYUEi6NzNzcGTcqA!/o/dNoAAAAAAAAA&save=1&d=1
            //  测试结果:r是原图大小,丢失EXIF信息;b和o下载的图MD5一样,但尺寸不是原图尺寸,小很多.
            //
            // 测试有三种模式:m,r,o,b
            //      推测的解释有:
            //      o = origin
            //      b = big
            //      r = raw
            //      m = 
            // 最终确认:用o和r的方式下载.检测r超时,则放弃.
            console.log(item.src);
            var tmp = item.src.replace(/http:\/\/[^\/]+\/(psb\?.+)null&bo.+/g, 'http://r.photo.store.qq.com/$1');
            tmp += "&save=1&d=1";
            //
            photoUrlsOrigin += tmp.replace(/\/m\//g, '/o/');
            photoUrlsOrigin += '\r\n';
            //
            photoUrlsRaw += tmp.replace(/\/m\//g, '/r/');
            photoUrlsRaw += '\r\n';
        }
        console.log('Count:' + count);
        //console.log('photoUrlsOrigin:\n' + photoUrlsOrigin);
        //console.log('photoUrlsRaw:\n' + photoUrlsRaw);
        //
        downloadAsCsv(photoUrlsOrigin, "Origin");
        downloadAsCsv(photoUrlsRaw, "Raw");
    }
    )
    //();	
}
)
window.YeDoIt = function() {
    console.log("绑定OnKeyDown中...");
    try {
        var doc = mainFrame.document;
        var items = doc.getElementsByClassName("f_size hideTop");
        if (items.length == 0) {
            window.document.title = "没找到匹配按钮!";
            console.log("没找到匹配按钮!绑定OnKeyDown失败!");
            return;
        }
        doc.onmousemove = function(e) {
            window.YeNowElement = e.srcElement;
        }
        ;
        doc.onkeydown = function(e) {
            switch (e.keyCode) {
            case 8:
                {
                    //修复：按Back键返回时，不刷新已读邮件状态的问题。
                    var btns = doc.getElementsByClassName('btn_back');
                    if (!btns || btns.length == 0)
                        break;

                    if (e.returnValue)
                        e.returnValue = false;

                    if (e.preventDefault)
                        e.preventDefault();

                    btns[0].click();
                    return false;
                }
                break;
            case 27:
                {
                    console.log("40特定功能执行中...");
                    try {
                        var doc2 = mainFrame.document;
                        var items2 = doc2.getElementsByClassName("f_size hideTop");
                        var isTrue = false;
                        for (var i = 0; i < items2.length; i++) {
                            if (items2[i].parentElement.parentElement.style.display != "none") {
                                console.log("功能1,执行成功!");
                                items2[i].click();
                                isTrue = true;
                            }
                        }
                        if (!isTrue) {
                            //无法显示图片！
                            setTimeout(function() {
                                window.YeNowElement.click();
                            }, 1);
                            //console.log(window.YeNowElement.onclick());
                            console.log("功能2,执行成功!");
                        }
                    } catch (ex) {
                        console.log(ex.toString());
                    }
                    console.log("49特定功能执行完毕!");
                }
                break;
            }
        }
        ;
    } catch (e) {
        window.document.title = e;
        console.log(e.toString());
    }
    console.log("39绑定OnKeyDown成功！");
}
;

console.log("绑定onload中...");
window.YeMainFrame = document.getElementById('mainFrame');
//if (!window.oldMainFrameOnLoad)
//    window.oldMainFrameOnLoad = YeMainFrame.onload;
YeMainFrame.onload = function(event) {
    console.log("20onload Ing...");
    try {
        //window.oldMainFrameOnLoad(event);
        window.YeDoIt();
    } catch (ex) {
        console.log(ex.toString());
    }
    console.log("29onloaded!");
}
;
console.log("绑定onload OK!");
